package com.example.lvsuo.utils;

import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.StringUtils;

import java.util.*;

public class AutoGeneratorUtils {
    public static void main(String[] args) {
        AutoGenerator ag = new AutoGenerator();
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java/");
        gc.setAuthor("心心念念");
        gc.setOpen(false);
        ag.setGlobalConfig(gc);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql:///lvsuo?serverTimezone=GMT%2B8");
        dsc.setUsername("root");
        dsc.setPassword("root");
        ag.setDataSource(dsc);

        PackageConfig pc = new PackageConfig();
        pc.setParent("");
        ag.setPackageInfo(pc);

        StrategyConfig strategy = new StrategyConfig();
        //包的命名规则，使用驼峰规则
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //列的名称，使用驼峰规则
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //是否使用lombok
        strategy.setEntityLombokModel(true);
        //驼峰命名
        strategy.setRestControllerStyle(true);
        ag.setStrategy(strategy);

        ag.execute();
    }

    @Test
    public void demo(){
        GrantedAuthority g=new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return "admin";
            }
        };
        Set<GrantedAuthority> set=new HashSet<>();
        set.add(g);
        User principal = new User("wyq","123",true,true,true,true,set);
        Collection<GrantedAuthority> authorities = principal.getAuthorities();
        String s = StringUtils.collectionToCommaDelimitedString(authorities);
        String token = JwtUtils.createToken(principal.getUsername(), s);
        System.out.println(token);
        Map<String, Claim> check = JwtUtils.check(token);
        System.out.println(check);
    }
}
